package com.source.boot01.uploadcontroller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**  https://www.cnblogs.com/dsxie/p/12992006.html
 * @Author wengqy
 * @Date 2021/1/12
 */
@RestController
public class UploadController {
     SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("/yyyy/MM/dd/");


    /**
     *
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public String  uploadPic(MultipartFile file, HttpServletRequest request) throws IOException {
        String contextPath = request.getContextPath();
        System.out.println("contextPath"+contextPath);
        String originalFilename = file.getOriginalFilename();
        String  newName = UUID.randomUUID().toString()+ originalFilename.substring(originalFilename.lastIndexOf("."));
        String realPath = request.getServletContext().getRealPath("/");
        String format = simpleDateFormat.format(new Date());
        String filePath = realPath + format;
        File   f  = new File(filePath);
        if(!f.exists()){
            f.mkdirs();
        }

        file.transferTo(new File(filePath,newName));
        System.out.println("filePath: "+filePath);// C:\Users\source\AppData\Local\Temp\tomcat-docbase.8080.2057139114830615872\/2021/01/12/

        String path =  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+format+newName;
         // http:\localhost:8080/2021/01/12/e28ff9e2-bb24-47d6-9061-43253f74c112.jpg
        return path;
    }


    @PostMapping("/multiupload")
    public String  uploadPic2(MultipartFile[] files, HttpServletRequest request) throws IOException {

        String realPath = request.getServletContext().getRealPath("/");
        String format = simpleDateFormat.format(new Date());


        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
            String  newName = UUID.randomUUID().toString()+ originalFilename.substring(originalFilename.lastIndexOf("."));

            String filePath = realPath + format;
            File   f  = new File(filePath);
            if(!f.exists()){
                f.mkdirs();
            }
            file.transferTo(new File(filePath,newName));
            System.out.println("filePath: "+filePath);// C:\Users\source\AppData\Local\Temp\tomcat-docbase.8080.2057139114830615872\/2021/01/12/

            String path =  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+format+newName;
            System.out.println(path);
            // http:\localhost:8080/2021/01/12/e28ff9e2-bb24-47d6-9061-43253f74c112.jpg
        }


        return "";


    }


    @PostMapping("/multiupload2")
    public String  uploadPic23(MultipartFile file1, MultipartFile file2, HttpServletRequest request) throws IOException {

        String realPath = request.getServletContext().getRealPath("/");
        String format = simpleDateFormat.format(new Date());



            String originalFilename1 = file1.getOriginalFilename();
            String  newName = UUID.randomUUID().toString()+ originalFilename1.substring(originalFilename1.lastIndexOf("."));

            String filePath = realPath + format;
            File   f  = new File(filePath);
            if(!f.exists()){
                f.mkdirs();
            }
            file1.transferTo(new File(filePath,newName));

            String path =  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+format+newName;
            System.out.println(path);
            // http:\localhost:8080/2021/01/12/e28ff9e2-bb24-47d6-9061-43253f74c112.jpg


        String originalFilename2 = file2.getOriginalFilename();
          newName = UUID.randomUUID().toString()+ originalFilename2.substring(originalFilename2.lastIndexOf("."));

         filePath = realPath + format;
          f  = new File(filePath);
        if(!f.exists()){
            f.mkdirs();
        }
        file2.transferTo(new File(filePath,newName));

        String path2 =  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+format+newName;
        System.out.println(path2);


        return "";


    }

//

    @PostMapping("/ajaxupload")
    public String  ajaxupload(MultipartFile file, HttpServletRequest request) throws IOException {

        System.out.println("参数："+request.getParameter("source")); // source
        System.out.println(file);
        System.out.println(file);
        String contextPath = request.getContextPath();
        System.out.println("contextPath"+contextPath);
        String originalFilename = file.getOriginalFilename();
        String  newName = UUID.randomUUID().toString()+ originalFilename.substring(originalFilename.lastIndexOf("."));
        String realPath = request.getServletContext().getRealPath("/");
        String format = simpleDateFormat.format(new Date());
        String filePath = realPath + format;
        File   f  = new File(filePath);
        if(!f.exists()){
            f.mkdirs();
        }

        file.transferTo(new File(filePath,newName));
        System.out.println("filePath: "+filePath);// C:\Users\source\AppData\Local\Temp\tomcat-docbase.8080.2057139114830615872\/2021/01/12/

        String path =  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+format+newName;
        System.out.println("path:  "+path);
        // http:\localhost:8080/2021/01/12/e28ff9e2-bb24-47d6-9061-43253f74c112.jpg
        return path;
    }

    @PostMapping("/jqueryupload")
    public String  jqueryupload(MultipartFile file, HttpServletRequest request) throws IOException {



        String contextPath = request.getContextPath();
        System.out.println("contextPath"+contextPath);
        String originalFilename = file.getOriginalFilename();
        String  newName = UUID.randomUUID().toString()+ originalFilename.substring(originalFilename.lastIndexOf("."));
        String realPath = request.getServletContext().getRealPath("/");
        String format = simpleDateFormat.format(new Date());
        String filePath = realPath + format;
        File   f  = new File(filePath);
        if(!f.exists()){
            f.mkdirs();
        }

        file.transferTo(new File(filePath,newName));
        System.out.println("filePath: "+filePath);// C:\Users\source\AppData\Local\Temp\tomcat-docbase.8080.2057139114830615872\/2021/01/12/

        String path =  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+format+newName;
        System.out.println("path:  "+path);
        // http:\localhost:8080/2021/01/12/e28ff9e2-bb24-47d6-9061-43253f74c112.jpg
        return path;
    }




}
